package com.ysalliance.getFan.organization.ui;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.zhouwei.library.CustomPopWindow;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ysalliance.getFan.organization.Bean.OrganizationBean;
import com.ysalliance.getFan.organization.Bean.SearchVideoBean;
import com.ysalliance.getFan.organization.LucasUtil;
import com.ysalliance.getFan.organization.R;
import com.ysalliance.getFan.organization.adapter.ClassroomAdapter;
import com.ysalliance.getFan.organization.adapter.SearchClassAdapter;
import com.ysalliance.getFan.organization.network.Network;
import com.ysalliance.getFan.organization.ui.base.LucasBaseActivity;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;


/**
 * 入驻页面
 * @author lucas
 * created at 2020-02-24 14:54
 */
public class SearchVideoActivity extends LucasBaseActivity {

    @BindView(R.id.et)
    EditText et;
    @BindView(R.id.tv_search)
    TextView tv_search;
    @BindView(R.id.recycle)
    RecyclerView recycle;



    private SearchClassAdapter searchClassAdapter;
    //视频类表
    private List<SearchVideoBean> list =  new ArrayList<>();

    @Override
    public int bindContentView() {
        return R.layout.activity_search_admin;
    }

    @Override
    public void afterView() {
        super.afterView();





        et.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        et.setInputType(EditorInfo.TYPE_CLASS_TEXT);
        KeyboardUtils.showSoftInput(et);






        recycle.setVisibility(View.INVISIBLE);
        //唱段列表Adapter初始化
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycle.setLayoutManager(linearLayoutManager);
        //设置列表默认动画效果
        recycle.setItemAnimator(new DefaultItemAnimator());
        searchClassAdapter = new SearchClassAdapter(R.layout.item_search_video, list);
        recycle.setAdapter(searchClassAdapter);
        //列表点击事件
        //列表点击事件
        searchClassAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox_select);
                checkBox.setChecked(true);

                Intent intent = new Intent();
                intent.putExtra("slected_video", list.get(position));
                setResult(RESULT_OK, intent);
                finish();
            }
        });




        initClick();

    }


    public void initClick() {
        //点击搜索
        et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    //执行搜索方法
                    getSearchVideo(et.getText().toString().trim());

                }
                return false;
            }
        });

        tv_search.setOnClickListener(v->{
            //执行搜索方法
            getSearchVideo(et.getText().toString().trim());
        });

    }


    //隐藏软键盘
    @Override
    protected void onPause() {
        super.onPause();
        KeyboardUtils.hideSoftInput(et);
    }







    /**  获取搜索视频   */
    public void getSearchVideo(String search_video_name){
        disposable = Network.getUtilApi()
                .getSearchVideo(search_video_name, "1")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<ResponseBody>() {
                    @Override
                    public void accept(@NonNull ResponseBody responseBody) throws Exception {
                        String res = responseBody.string();
                        LogUtils.json("添加任务时搜索课程的返回", res);



                        JSONObject jsonObject = new JSONObject(  res  );
                        if(LucasUtil.isok(jsonObject.optString("status"))){
                            Gson gson = new Gson();
                            JSONObject jsonObjectData = new JSONObject(  jsonObject.optString("data")  );
                            List<SearchVideoBean> listVideo2 = gson.fromJson(jsonObjectData.optString("results"), new TypeToken<List<SearchVideoBean>>(){}.getType());

                            if(listVideo2.size()>0){
                                recycle.setVisibility(View.VISIBLE);

                            }else {
                                recycle.setVisibility(View.GONE);
                            }

                            //更新列表
                            list.clear();
                            list.addAll(listVideo2);
                            searchClassAdapter.notifyDataSetChanged();

                        }else {
                            ToastUtils.showShort(jsonObject.optString("message"));
                        }


                    }

                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(@io.reactivex.annotations.NonNull Throwable throwable) throws Exception {
                        LogUtils.e(throwable.getMessage());
                        Toast.makeText(mContext,"网络请求失败！", Toast.LENGTH_SHORT).show();
                    }
                });
    }



}
